﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.Drawing.Printing;
using System.Collections.Specialized;
using LHS.Core.TableInfo;
using MS.Core.Constants.Objects;

namespace LHS.Win
{
    public partial class frmCapNhatAnh : Form
    {
        LHS.Core.Clinical.ClinicalController objController = new LHS.Core.Clinical.ClinicalController();
        // Methods
        public frmCapNhatAnh(string clrISN, string NgayKham)
        {
            this.InitializeComponent();
            this._clrISN = clrISN;
            this._NgayKham = NgayKham;
        }

        private void btAddPic_Click(object sender, EventArgs e)
        {
            if (this.tbMoTa.Text == "")
            {
                MessageBox.Show("Vui lòng nhập mô tả", "Lỗi nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            else if (this.picPatient.Image == null)
            {
                MessageBox.Show("Vui lòng chọn ảnh", "Lỗi nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            else
            {
                try
                {
                    MemoryStream stream = new MemoryStream();
                    this.picPatient.Image.Save(stream, this.picPatient.Image.RawFormat);
                    byte[] buffer = stream.GetBuffer();
                    stream.Close();
                    int num = 0;
                    if (this.cbPictureStatus.SelectedItem.ToString() == "Tai, Mũi xoang")
                    {
                        num = 0;
                    }
                    else if (this.cbPictureStatus.SelectedItem.ToString() == "Viêm họng, Họng-Hạ họng, Thanh quản")
                    {
                        num = 1;
                    }

                    ListDictionary param = new ListDictionary();
                    param.Add(InfoClinicalRecord.CLRISN, _clrISN);
                    param.Add(InfoScanPicture.SCPPICTURE, string.Empty);
                    param.Add(InfoScanPicture.SCPNOTE, this.tbMoTa.Text);
                    param.Add(InfoScanPicture.SCPIMAGE, buffer);
                    param.Add(InfoScanPicture.SCPPICTURESTATUS, num);

                    objController.Exec_StoreProc("sp_ScanPicture_Insert", param);

                    this.arrPicPath.Clear();
                    this.arrDescription.Clear();
                    this.arrISN.Clear();
                    this.tbMoTa.Text = "";
                    this.frmCapNhatAnh_Load(sender, e);
                    MessageBox.Show("Ảnh đã được lưu", "Chèn Hình", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                catch
                {
                    MessageBox.Show("Vui lòng chọn ảnh", "Lỗi Nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
            }
        }

        private void btBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "JPEG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif|Bitmap Files (*.bmp)|*.bmp";
            dialog.FilterIndex = 1;
            dialog.RestoreDirectory = true;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                if (this.MyImage != null)
                {
                    this.MyImage.Dispose();
                }
                this.MyImage = new Bitmap(dialog.FileName);
                this.picPatient.Image = this.MyImage;
                this.MyImage = null;
            }
        }

        private void btCapNhat_Click(object sender, EventArgs e)
        {
            if (this.tbMoTa.Text == "")
            {
                MessageBox.Show("Vui lòng nhập mô tả", "Lỗi nhập", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (this.picPatient.Image == null)
            {
                MessageBox.Show("Vui lòng chọn ảnh", "Lỗi Nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            else
            {
                try
                {
                    if (this.arrPicPath.Count == 0)
                    {
                        MessageBox.Show("Vui lòng chọn nút chèn ảnh","Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    MemoryStream stream = new MemoryStream();
                    this.picPatient.Image.Save(stream, this.picPatient.Image.RawFormat);
                    byte[] buffer = stream.GetBuffer();
                    stream.Close();
                    int num = 0;
                    if (this.cbPictureStatus.SelectedItem.ToString() == "Tai, Mũi xoang")
                    {
                        num = 0;
                    }
                    else if (this.cbPictureStatus.SelectedItem.ToString() == "Viêm họng, Họng-Hạ họng, Thanh quản")
                    {
                        num = 1;
                    }

                    ListDictionary param = new ListDictionary();
                    param.Add(InfoScanPicture.SCPISN, this.arrPicPath[this.index].ToString());
                    param.Add(InfoScanPicture.SCPPICTURE, string.Empty);
                    param.Add(InfoScanPicture.SCPNOTE, this.tbMoTa.Text);
                    param.Add(InfoScanPicture.SCPIMAGE, buffer);
                    param.Add(InfoScanPicture.SCPPICTURESTATUS, num);

                    objController.Exec_StoreProc("sp_ScanPicture_Update", param);

                    this.arrPicPath.Clear();
                    this.arrDescription.Clear();
                    this.arrISN.Clear();
                    this.tbMoTa.Text = "";
                    this.frmCapNhatAnh_Load(sender, e);
                    MessageBox.Show("Ảnh đã được lưu", "Chèn Hình", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                catch (Exception)
                {
                    int num2 = 0;
                    if (this.cbPictureStatus.SelectedItem.ToString() == "Tai, Mũi xoang")
                    {
                        num2 = 0;
                    }
                    else if (this.cbPictureStatus.SelectedItem.ToString() == "Viêm họng, Họng-Hạ họng, Thanh quản")
                    {
                        num2 = 1;
                    }
                    if (arrPicPath.Count == 0)
                        return;
                    ListDictionary param = new ListDictionary();
                    param.Add(InfoScanPicture.SCPISN, this.arrPicPath[this.index].ToString());
                    param.Add(InfoScanPicture.SCPPICTURE, string.Empty);
                    param.Add(InfoScanPicture.SCPNOTE, this.tbMoTa.Text);
                    param.Add(InfoScanPicture.SCPPICTURESTATUS, num2);

                    objController.Exec_StoreProc("sp_ScanPictureNoPicture_Update", param);

                    this.arrPicPath.Clear();
                    this.arrDescription.Clear();
                    this.arrISN.Clear();
                    this.tbMoTa.Text = "";
                    this.frmCapNhatAnh_Load(sender, e);
                    MessageBox.Show("Ảnh đã được lưu", "Chèn Hình", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
            }
        }

        private void btNext_Click(object sender, EventArgs e)
        {
            this.index++;
            this.LoadPicture();
        }

        private void btPrevious_Click(object sender, EventArgs e)
        {
            this.index--;
            this.LoadPicture();
        }

        private void btPrint_Click(object sender, EventArgs e)
        {
            if (this.picPatient.Image != null)
            {
                PrintDocument document = new PrintDocument();
                PrintDialog dialog = new PrintDialog();
                dialog.Document = document;
                dialog.ShowDialog();
                try
                {
                    document.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
                    document.Print();
                }
                catch
                {
                    MessageBox.Show("Quá trình in ảnh đã được huỷ", "Hủy In Ảnh", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
            }
            else
            {
                MessageBox.Show("Vui lòng chọn ảnh", "", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }

        private void btThoat_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            try
            {
                List<ExpressionObject> param = new List<ExpressionObject>();
                param.Add(new ExpressionObject(InfoScanPicture.SCPISN, this.arrPicPath[this.index].ToString()));
                objController.DeleteRow(InfoScanPicture.TABLENAME, param);

                this.arrPicPath.Clear();
                this.arrDescription.Clear();
                this.arrISN.Clear();
                this.tbMoTa.Text = "";
                this.frmCapNhatAnh_Load(sender, e);
                MessageBox.Show("Ảnh đã xóa lưu", "Xóa Hình", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            catch (Exception ex)
            {
                Class.ShowException(ex);
            }
        }
        private void frmCapNhatAnh_Load(object sender, EventArgs e)
        {
            this.index = 0;
            string str = this._clrISN;
            this.cbPictureStatus.SelectedIndex = 0;
            if (str != string.Empty)
            {
                this.lbNgayKham.Text = this._NgayKham;
                List<ExpressionObject> param = new List<ExpressionObject>();
                param.Add(new ExpressionObject( InfoScanPicture.CLRISN,str));
                DataTable dt= objController.GetData(InfoScanPicture.TABLENAME, param);

                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        this.arrISN.Add(dt.Rows[i][0].ToString());
                        this.arrDescription.Add(dt.Rows[i][InfoScanPicture.SCPNOTE].ToString());
                        this.arrPicPath.Add(dt.Rows[i][InfoScanPicture.SCPISN].ToString());
                        this.arrPictureStatus.Add(dt.Rows[i][InfoScanPicture.SCPPICTURESTATUS].ToString());
                    }
                    if ((this.arrISN.Count == 0) || (this.arrISN.Count == 1))
                    {
                        this.btNext.Enabled = false;
                        this.btPrevious.Enabled = false;
                    }
                    else
                    {
                        this.btNext.Enabled = true;
                        this.btPrevious.Enabled = true;
                    }
                    if (this.arrISN.Count > 0)
                    {
                        this.LoadPicture();
                        this.btCapNhat.Enabled = true;
                    }
                    else
                    {
                        this.btCapNhat.Enabled = false;
                    }
                }
            }
        }

        private void LoadPicture()
        {
            if (this.arrPicPath.Count == 1)
            {
                this.btPrevious.Enabled = false;
            }
            else if (this.index == (this.arrPicPath.Count - 1))
            {
                this.btNext.Enabled = false;
                this.btPrevious.Enabled = true;
            }
            else if (this.index <= 0)
            {
                this.btPrevious.Enabled = false;
                this.btNext.Enabled = true;
            }
            else
            {
                this.btPrevious.Enabled = true;
                this.btNext.Enabled = true;
            }
           
            List<ExpressionObject> param = new List<ExpressionObject>();
            param.Add(new ExpressionObject( InfoScanPicture.SCPISN, this.arrPicPath[this.index].ToString()));
            DataTable dt = objController.GetData(InfoScanPicture.TABLENAME, param);

            if (dt != null && dt.Rows.Count > 0)
            {
                this.tbMoTa.Text = this.arrDescription[this.index].ToString();
                if (this.arrPictureStatus[this.index].ToString() == "0")
                {
                    this.cbPictureStatus.SelectedItem = "Tai, Mũi xoang";
                }
                else if (this.arrPictureStatus[this.index].ToString() == "1")
                {
                    this.cbPictureStatus.SelectedItem = "Viêm họng, Họng-Hạ họng, Thanh quản";
                }
                byte[] buffer = (byte[])dt.Rows[0][InfoScanPicture.SCPIMAGE];
                MemoryStream stream = new MemoryStream(buffer);
                this.picPatient.Image = Image.FromStream(stream);
                stream.Close();
            }
        }

        private void pd_PrintPage(object sender, PrintPageEventArgs e)
        {
            Rectangle pageBounds = e.PageBounds;
            Size size = this.picPatient.Image.Size;
            float num = Math.Min((float)(300f / ((float)size.Width)), (float)(300f / ((float)size.Height)));
            e.Graphics.DrawImage(this.picPatient.Image, (float)(pageBounds.X + ((pageBounds.Width - (size.Width * num)) / 2f)), (float)(pageBounds.Y + ((pageBounds.Height - (size.Height * num)) / 2f)), (float)(size.Width * num), (float)(size.Height * num));
            e.HasMorePages = false;
        }

    }
}